package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.entity.Category;
import com.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: reggie
 * @author:
 * @create: 2022-09-27 22:00
 **/
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分类管理的查询
     * */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        Page<Category> categoryPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //进行排序
        wrapper.orderByAsc(Category::getSort);
        categoryService.page(categoryPage,wrapper);
        return R.success(categoryPage);
    }
    /**
     * 分类管理的修改
     * */
    @PutMapping
    public R<String> updateCategory( @RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改分类成功！");
    }
    /**
     * 添加菜品的分类
     *
     * */
    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功！");
    }
    /**
     * 删除菜品的分类,如果有菜品关联了分类不能删除
     * 相关联的表：Dish(餐品)和SetmealDish(套餐表)
     * */
    @DeleteMapping
    public R<String> deleteCategory(Long ids){
        categoryService.removeById(ids);
        return R.success("删除成功！");

    }

    /**
     * 添加菜品，回显菜品的分类
     * */
    @GetMapping("/list")
    public R<List> list(String type){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type!=null,Category::getType,type);
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);

    }
}
